package com.example.as_sum.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class SharePerferenceUtil {

	private static String DATA_PASSWORD="lifaran124874";
	private static String name="ran";
	private static String MOBILE="MOBILE";
	/**
	 * 
	 * @param context
	 * @param mobile
	 */
	public static boolean putMobile(Context context, String mobile) {
		SharedPreferences sp = context.getSharedPreferences(name,
				Context.MODE_PRIVATE);
		String result=encode(mobile);//加密
		return sp.edit().putString(MOBILE, result).commit();
	}


	public static boolean deteleAll(Context context) {
		SharedPreferences sp = context.getSharedPreferences(name,
				Context.MODE_PRIVATE);
		return sp.edit().clear().commit();
	}

	public static String getMobile(Context context) {
		SharedPreferences sp = context.getSharedPreferences(name,
				Context.MODE_PRIVATE);
		String result=decode(sp.getString(MOBILE, ""));//解密
		return result;
	}
	/**
	 * 加密
	 * @param result 原文
	 * @return 密文
	 */
	public static String encode(String result){
		String code= result;
		try {
			code = AESUtil.encrypt(DATA_PASSWORD, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return code;
	}

	/**
	 * 解密
	 * @param result  密文
	 * @return 原文
	 */
	public static String decode(String result){
		if(!TextUtils.isEmpty(result)) {
			String code = result;
			try {
				code = AESUtil.decrypt(DATA_PASSWORD, result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return code;
		}else{
			return "";
		}
	}
}
